import Axios from 'axios'
import Cookies from 'js-cookie'
// import qs from 'qs'
import {Message} from "element-ui";

const axios = Axios.create(
    {
        timeout: 80000,
        withCredentials: true
    }
)
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.common['Content-Type'] = 'application/json;'

axios.interceptors.request.use(config => {
        // if (config.data) {
        //     config.data = JSON.stringify(config.data)
        // }
        if (localStorage.token) {
            config.headers.Authorization = localStorage.token
        }
        if (Cookies.get('token')) {
            config.headers['token'] = Cookies.get('token')
        }
        // var token = cookie.get('token');
        // if (token) {
        //     config.headers['token'] = token;
        // }
        return config
    },
    error => {
        if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|JUC|WebOS)/i)) {
            console.log(navigator.userAgent)
        } else {
            Message({
                showClose: true,
                message: error,
                type: 'error'
            })

        }
        return Promise.reject(error.data)
    }
)

axios.interceptors.response.use(
    res => {
        if (res.data.code !== '000000') {
            Message({
                message: res.data.msg,
                type: 'error',
                offset: 50,
                duration: 1500
            })
        }
        return res
    },
    function (error) {
        if (error.response && error.response.status && error.response.status === 403) {
            console.log(error)
        } else {
            Promise.reject(error)
        }
    }
)

axios.CancelToken = Axios.CancelToken
export default axios
